
library(Zelig); library(apsrtable); library(plyr)


# WORDFISH DATA -----------------------------------------------------------

## Read data ---------------------------------------------------------------

load("wordfish_data.RData")
dta <- dta_wordfish

## Main model --------------------------------------------------------------

# The Parliament.w.s and Manifesto.w.s positions are used
# For the parliament, this concerns the mean of the scores for separate years, 
# weighted by the length of a contribution for each year
# (e.g. if positions are given by p = c(.5,.6.,.7) and weights by 
# w=c(10000, 10000, 20000), then the weighted positions are p * w / w)
# Next, both the parliament and manifesto scores were standardized

#Unweighted model
z.p.c.u = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(country), data=dta, model="ls")
print(summary(z.p.c.u))

# Weighted by counts
z.p.c.w = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(country), data=dta, model="ls", weights=dta$counts.x)
print(summary(z.p.c.w))

# Frontbench model (weighted)
dta.fb = subset(dta, bench == "Frontbench")
z.p.c.fb.u = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(country), data=dta.fb, model="ls")
z.p.c.fb.w = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(country), data=dta.fb, model="ls")

print(summary(z.p.c.fb.w))

# Nice formatting
apsrtable(z.p.c.w,z.p.c.fb.w, digits=3)

## Over time ---------------------------------------------------------------

combined.pr = dta
combined.pr$year = as.numeric(substring(combined.pr$period,0,4))
combined.pr$period = car::recode(as.character(combined.pr$period), "'1952-1956'='1950s';'1955-1959'='1950s';'1959-1963'='1960s';'1966-1970'='1960s';'1974-1979'='1970s';'1972-1977'='1970s';'1983-1987'='1980s';'1982-1986'='1980s';'1992-1997'='1990s';'1994-1998'='1990s';'2001-2005'='2000s';'2003-2006'='2000s'", levels=c("1950s", "1960s", "1970s", "1980s", "1990s", "2000s"))

z.p.h = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(period), data=combined.pr, model="ls")

z.p.hc = zelig(Parliament.w.s ~ Manifesto.w.s * as.factor(period), data=combined.pr, model="ls", by="country")

apsrtable(z.p.h,z.p.hc[[2]],z.p.hc[[1]],digits=3,stars="default")

## Party level factors -----------------------------------------------------

z.pd.se = zelig(distance01 ~ Saliency.Manifesto.n + Extreme01 + Partyextremism01, data=dta, model="ls")
print(summary(z.pd.se))

z.pd.sec = zelig(distance01 ~ Saliency.Manifesto.n + Extreme01 + Partyextremism01, data=dta, model="ls", by="country")
print(summary(z.pd.sec))

apsrtable(z.pd.se,z.pd.sec$UK, z.pd.sec$NL, digits=3, stars="default")



# SALIENCY DATA -----------------------------------------------------------


## Read data ---------------------------------------------------------------

load("saliency_data.Rdata")


means <- plyr::ddply(dta_saliency, .(country, period, category), plyr::summarise, mean.Manifesto=mean(Manifesto), mean.Parliament=mean(Parliament))
dta_saliency <- merge(dta_saliency, means)


## Main model ------------------------------------------------------------

z.s.c1 = zelig(Parliament ~ Manifesto*country,data=dta_saliency,model="ls")
z.s.c2 = zelig(Parliament ~ Manifesto*country+country*mean.Parliament,data=dta_saliency,model="ls")
z.s.c3 = zelig(Parliament ~ Manifesto*country+country*mean.Manifesto,data=dta_saliency,model="ls")
apsrtable(z.s.c1,z.s.c2, z.s.c3,digits=3,stars="default")



## Over time ---------------------------------------------------------------

dta_saliency_period = dta_saliency
dta_saliency_period$year = as.numeric(substring(dta_saliency_period$period,0,4))
dta_saliency_period$period = car::recode(as.character(dta_saliency_period$period), "'1952-1956'='1950s';'1955-1959'='1950s';'1959-1963'='1960s';'1966-1970'='1960s';'1974-1979'='1970s';'1972-1977'='1970s';'1983-1987'='1980s';'1982-1986'='1980s';'1992-1997'='1990s';'1994-1998'='1990s';'2001-2005'='2000s';'2003-2006'='2000s'", levels=c("1950s", "1960s", "1970s", "1980s", "1990s", "2000s"))

z.s.h = zelig(Parliament ~ Manifesto * period, data=dta_saliency_period,model="ls")
z.s.hc = zelig(Parliament ~ Manifesto * period, data=dta_saliency_period,model="ls", by="country")

apsrtable(z.s.h,z.s.hc[[2]],z.s.hc[[1]],digits=3,stars="default")






